perf(decode): fast-path 1/2-byte varints (+12% codec decode) - #127
Merged
Conversation
decodeVarint ran a general loop with a per-byte bounds check and two overflow checks for every value. Add branch-predicted 1-byte and 2-byte fast paths (covering the vast majority of real deltas); keep the careful general path for 3+ byte varints and all truncation/overflow/NaN semantics unchanged. Decode throughput on DATA/ouster_os1.mcap (codec-only, 94 msgs): V4: ~1462 -> ~1645 MB/s (+12.5%) V5: ~1303 -> ~1476 MB/s (+13.3%) Decoded-output FNV-1a fingerprint unchanged (0x75b0cc1fbc0457e9); ctest green. Also adds a --hash correctness fingerprint to mcap_codec_benchmark. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Adds DecodeVarintMatchesOracleExhaustiveAndRandom: compares the optimized decodeVarint against an in-test oracle (the original loop-only implementation, verbatim) over all 1- and 2-byte prefixes with every truncation length, a boundary sweep of 3-byte prefixes, and 1M randomized longer/malformed inputs. Any divergence in value, byte count, or throw/no-throw fails the test. Runs in ~2s under Debug+ASan. Proves the 1/2-byte fast paths preserve exact varint semantics (truncation, overflow, NaN marker, zigzag). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
From /pr-review + /simplify: - Reduce the differential test's random sweep 1M -> 200k. The 1/2-byte fast paths are already proven exhaustively and the 3+ byte path is original code, so the sweep is supplementary; this keeps the ASan/Debug ctest ~1s. - Reject --hash without --decode-replay. The fingerprint pass lives inside the decode-replay block, so --hash alone silently printed nothing — a footgun for a correctness gate. Now errors clearly. ctest green; decoded-output fingerprint unchanged (0x75b0cc1fbc0457e9). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
decodeVarintis the hottest primitive on the decode path (~8 calls per point for a typical LIDAR cloud). It ran a general loop with a per-byte bounds check and two overflow checks for every value. This PR adds branch-predicted 1-byte and 2-byte fast paths — covering the vast majority of real deltas — while keeping the careful general path for 3+ byte varints and all truncation/overflow/NaN semantics unchanged.The motivation came from profiling decode with
perf: after a stage ofdecodeVarintcalls dominate both the integer fields and the inlined XYZ float path, the redundant per-byte checks were a measurable tax.Results (codec-only decode,
DATA/ouster_os1.mcap, 94 msgs)Measured with
mcap_codec_benchmark --decode-replay --mode <V4/V5> --decode-repeat 40(5 runs each, tight ±0.5% variance). The general (3+ byte) path is the original code byte-for-byte; only the 1- and 2-byte paths are new.Correctness
DecodeVarintMatchesOracleExhaustiveAndRandom: compares the optimized decoder against an in-test oracle (the original implementation, verbatim) over all 1- and 2-byte inputs × every truncation length, a boundary sweep of 3-byte inputs, and 200k randomized longer/malformed inputs. Zero divergence in value, byte count, or throw/no-throw. Runs ~1s under Debug/ASan.0x75b0cc1fbc0457e9) — exposed via a newmcap_codec_benchmark --hashcorrectness gate.ctestgreen. Independently reviewed (adversarial differential sweep up to 67M cases under ASan, zero mismatches; bounds-safety verified —buf[1]is read only under a short-circuitedmax_size >= 2guard, so no out-of-bounds read on truncated input).Scope & notes
encodeVarint64) and the wire format are untouched — round-trip contract preserved.encodeVarint64's zigzag (value << 1on negative), and theINT64_MIN↔ NaN-marker collision. Neither arises from real quantized/delta point data.Commits
perf(decode): fast-path 1/2-byte varints indecodeVarint(+--hashbenchmark tooling)test(decode): differential oracle test for the fast pathsrefactor(decode): address review — trim test sweep to 200k, reject--hashwithout--decode-replay🤖 Generated with Claude Code